﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace University.Workshop.Business
{
    public static class ValidateFields
    {
        private static bool vCi = false;
        private static bool vFirstName = false;
        private static bool vLastName = false;
        //private static bool vName = false;
        private static bool vAddress = false;
        private static bool vCellPhone = false;
        private static bool vPhone = false;
        private static bool vEmail = false;
        private static bool vUserName = false;
        private static bool vPassword = false;
        private static bool vConfirPassword = false;
        private static bool vNumberInt = false;
        private static bool vNumberFloat = false;
        public static bool ValidateNumberFields() 
        {
            if (vNumberInt && vNumberFloat)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool ValidatingFieldsNewUser()
        {
            if (vCi && vFirstName && vLastName && vAddress && vCellPhone && vPhone && vEmail && vUserName && vPassword && vConfirPassword)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool ValidatingFieldsNewPatient() 
        {
            if (vCi && vFirstName && vLastName && vCellPhone && vPhone && vEmail)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool ValidatingFieldsChangePassword() 
        {
            if (vPassword && vConfirPassword)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string CIValidation(string ci)
        {
            string ciPattern = "^\\d{7,8}$";
            if (Regex.IsMatch(ci, ciPattern))
            {
                vCi = true;
                return null;
            }
            else
            {
                vCi = false;
                return "Error CI";
            }
        }
        public static string FirstNameValidation(string firstName)
        {
            string firstNamePattern = "[0-9_=+,;:*!@#$%^&<>?¡~¿\\.\\-]";
            if (!Regex.IsMatch(firstName, firstNamePattern) && !string.IsNullOrEmpty(firstName))
            {
                vFirstName = true;
                return null;
            }
            else
            {
                vFirstName = false;
                return "Error First Name";
            }
        }
        public static string NameValidation(string name)
        {
            string namePattern = "[0-9_=+,;:*!@#$%^&<>?¡~¿\\.\\-']";
            if (!Regex.IsMatch(name, namePattern) && !string.IsNullOrEmpty(name))
            {
                vFirstName = true;
                return null;
            }
            else
            {
                vFirstName = false;
                return "Error: No permit numbers, or special characters. This field only allows letters.";
            }
        }
        public static string LastNameValidation(string lastName)
        {
            string lastNamePattern = "[0-9_=+,;:*!@#$%^&<>?¡~¿\\.\\-]";
            if (!Regex.IsMatch(lastName, lastNamePattern) && !string.IsNullOrEmpty(lastName))
            {
                vLastName = true;
                return null;
            }
            else
            {
                vLastName = false;
                return "Error Last Name";
            }
        }
        public static string AddressValidation(string address)
        {
            string addressPattern = "[_=+,;:*!@$%^&<>?¡~¿\\-]";
            if (!Regex.IsMatch(address, addressPattern) && !string.IsNullOrEmpty(address))
            {
                vAddress = true;
                return null;
            }
            else
            {
                vAddress = false;
                return "Error Address";
            }
        }
        public static string CellPhoneValidation(string cellPhone)
        {
            string cellPhonePattern = "^\\d{8,8}$";
            if (Regex.IsMatch(cellPhone, cellPhonePattern))
            {
                vCellPhone = true;
                return null;
            }
            else
            {
                vCellPhone = false;
                return "Error Cell Phone";
            }
        }
        public static string PhoneValidation(string phone)
        {
            string phonePattern = "^\\d{7,8}$";
            if (Regex.IsMatch(phone, phonePattern))
            {
                vPhone = true;
                return null;
            }
            else
            {
                vPhone = false;
                return "Error phone";
            }
        }
        public static string EmailValidation(string email)
        {
            string emailPattern = "[a-zA-Z0-9\\.\\-_]+@[a-zA-Z]+\\.[a-zA-Z]+";
            if (Regex.IsMatch(email, emailPattern))
            {
                vEmail = true;
                return null;
            }
            else
            {
                vEmail = false;
                return "Error Email";
            }
        }
        public static string UserNameValidation(string userName)
        {
            string userNamePattern = "[_=+,;:!$%^&<>?¡~¿\\-\\.']";
            if (!Regex.IsMatch(userName, userNamePattern) && !string.IsNullOrEmpty(userName))
            {
                if (UserManager.UserExists(userName))
                {
                    vUserName = false;
                    return "the user already exists";

                }
                else
                {
                    vUserName = true;
                    return null;
                }
            }
            else
            {
                vUserName = false;
                return "Error User Name";
            }
        }
        public static string PasswordValidation(string password)
        {
            string passwordPattern = "[_=+,;:!$%^&<>?¡~¿\\-\\.']";
            if (!Regex.IsMatch(password, passwordPattern) && (!string.IsNullOrEmpty(password)) && password.Length>5)
            {
                vPassword = true;
                return null;
            }
            else
            {
                vPassword = false;
                return "Error Password";
            }
        }
        public static string ConfirmPasswordValidation(string confirmPassword)
        {
            string confirmPasswordPattern = "[_=+,;:!$%^&<>?¡~¿\\-\\.']";
            if (!Regex.IsMatch(confirmPassword, confirmPasswordPattern) && (!string.IsNullOrEmpty(confirmPassword)) && confirmPassword.Length>5)
            {
                vConfirPassword = true;
                return null;
            }
            else
            {
                vConfirPassword = false;
                return "Error Password";
            }
        }

        public static bool SearchFieldValidation(string search)
        {
            string searchPattern = "[_=+,;:!$%^&<>?¡~¿\\-\\.']";
            if (!Regex.IsMatch(search, searchPattern)) 
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string NumbersInt(string number)
        {
            string numberPattern = "^\\d{1,6}$";
            if (Regex.IsMatch(number, numberPattern))
            {
                vNumberInt = true;
                return null;
            }
            else
            {
                vNumberInt = false;
                return "Error: Unable to permiter letters or special characters. This field only allows numbers.";
            }
        }
        public static string NumbersFloat(string number)
        {
            string numberPattern = "^\\d{1,6}[0-9\\.]*";
            if (Regex.IsMatch(number, numberPattern))
            {
                vNumberFloat = true;
                return null;
            }
            else
            {
                vNumberFloat = false;
                return "Error: Unable to permiter letters or special characters. This field only allows numbers."; 
            }
        }
    }
}
